package cn.iocoder.idata.module.idata.controller.admin.datalog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.IOException;

import cn.iocoder.idata.framework.common.pojo.PageResult;
import cn.iocoder.idata.framework.common.pojo.CommonResult;
import static cn.iocoder.idata.framework.common.pojo.CommonResult.success;

import cn.iocoder.idata.framework.excel.core.util.ExcelUtils;

import cn.iocoder.idata.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.idata.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.idata.module.idata.controller.admin.datalog.vo.*;
import cn.iocoder.idata.module.idata.dal.dataobject.datalog.DataLogDO;
import cn.iocoder.idata.module.idata.convert.datalog.DataLogConvert;
import cn.iocoder.idata.module.idata.service.datalog.DataLogService;

@Tag(name = "管理后台 - 日志")
@RestController
@RequestMapping("/idata/data-log")
@Validated
public class DataLogController {

    @Resource
    private DataLogService dataLogService;


    @PostMapping("/create")
    @Operation(summary = "创建日志")
    @PreAuthorize("@ss.hasPermission('idata:data-log:create')")
    public CommonResult<Long> createDataLog(@Valid @RequestBody DataLogCreateReqVO createReqVO) {
        createReqVO.setIncomeTime(LocalDateTime.now());
        return success(dataLogService.createDataLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新日志")
    @PreAuthorize("@ss.hasPermission('idata:data-log:update')")
    public CommonResult<Boolean> updateDataLog(@Valid @RequestBody DataLogUpdateReqVO updateReqVO) {
        dataLogService.updateDataLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('idata:data-log:delete')")
    public CommonResult<Boolean> deleteDataLog(@RequestParam("id") Long id) {
        dataLogService.deleteDataLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('idata:data-log:query')")
    public CommonResult<DataLogRespVO> getDataLog(@RequestParam("id") Long id) {
        DataLogDO dataLog = dataLogService.getDataLog(id);
        return success(DataLogConvert.INSTANCE.convert(dataLog));
    }

    @GetMapping("/list")
    @Operation(summary = "获得日志列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('idata:data-log:query')")
    public CommonResult<List<DataLogRespVO>> getDataLogList(@RequestParam("ids") Collection<Long> ids) {
        List<DataLogDO> list = dataLogService.getDataLogList(ids);
        return success(DataLogConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得日志分页")
    @PreAuthorize("@ss.hasPermission('idata:data-log:query')")
    public CommonResult<PageResult<DataLogRespVO>> getDataLogPage(@Valid DataLogPageReqVO pageVO) {
        PageResult<DataLogDO> pageResult = dataLogService.getDataLogPage(pageVO);
        return success(DataLogConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出日志 Excel")
    @PreAuthorize("@ss.hasPermission('idata:data-log:export')")
    @OperateLog(type = EXPORT)
    public void exportDataLogExcel(@Valid DataLogExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DataLogDO> list = dataLogService.getDataLogList(exportReqVO);
        // 导出 Excel
        List<DataLogExcelVO> datas = DataLogConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "日志.xls", "数据", DataLogExcelVO.class, datas);
    }

}
